#!/bin/bash
#
# This script is generating a zap schema diagram out of a sqlite file.
#
# It assumes that you have installed a schemacrawler tool and few other things.
# You can find schemacrawler at: https://www.schemacrawler.com/
#
# You need to have graphviz package installed for this to work correctly.

# It also has only ever been tested on Linux.
#

SC_HOME=~/schemacrawler/latest
SC_BIN=${SC_HOME}/_schemacrawler/bin/schemacrawler.sh

SQLITE=~/.zap/self-check.sqlite
SVG=zap-schema.svg

if [ ! -x ${SC_BIN} ]; then
    echo "Can't find schemacrawler at ${SC_BIN}."
    echo "Please make sure schemacrawler is properly installed."
    echo ""
    echo "You can download it from: https://www.schemacrawler.com/"
    exit
fi

echo "Creating schema diagram to ${SVG} from database at ${SQLITE} ..."

echo "First run self-check to make sure database is present..."
npm run self-check

if [ ! -s ${SQLITE} ]; then
    echo ""
    echo "Can't locate ${SQLITE} file. Database file is required to read schema from."
    exit
fi

# Generate the dot file using the schemacrawler
${SC_BIN} --server sqlite --database=${SQLITE} --command=schema --info-level=standard --output-format=scdot --output-file=zap-schema.dot

if [ ! $? -eq 0 ]; then
    echo ""
    echo "Error executing schemacrawler from ${SC_BIN}"
    echo "Please make sure schemacrawler is properly installed."
    echo ""
    echo "You can download it from: https://www.schemacrawler.com/"
    exit
fi

# Fix the background of all HTML tables to white
sed -i 's/color="#888888"/color="#888888" bgcolor="#ffffff"/g' zap-schema.dot

# Fix the overall background of the image to transparent
sed -i 's/rankdir="RL"/rankdir="RL"\n    bgcolor="transparent"/g' zap-schema.dot

# Make edges black and bold
sed -i 's/edge \[/edge\[\n    color="black"\n    style="bold"/g' zap-schema.dot

# Remove the date stamp, so we don't create unnecessary commits
sed -i '/.*td align=.left..202.*/d' zap-schema.dot
sed -i 's/>generated on/ colspan=\"2\">ZAP schema, Copyright (c) 2020 Silicon Labs, released under Apache 2.0 license./g' zap-schema.dot

# Convert the dot to svg using the "dot" tool from graphviz
dot -Tsvg zap-schema.dot -o ${SVG}
if [ ! $? -eq 0 ]; then
    echo ""
    echo "Error executing 'dot' command from graphviz package."
    echo "Please make sure graphviz is installed and on your PATH."
    exit
fi

echo "Diagram created: ${SVG}"
